﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using ChangDiTools.Tool;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace ChangDiTools.Demo {
    public class UCSTables {
        [CommandMethod("NewUCS")]
        public void NewUCS()
        {
            string ucsName = "NewUCS";
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = db.GetEditor();
            using (var tr=db.TransactionManager.StartTransaction())
            {
                ObjectId ucsId = db.AddUCS(ucsName);//新建UCS
                db.SetCurrentUCS(ucsName);//设置为当前UCS
                tr.Commit();
            }
        }

        [CommandMethod("ModUCS")]
        public void ModUCS()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = db.GetEditor();
            //提示用户输入ucs新的原点
            PromptPointOptions opt=new PromptPointOptions("\n请指定ucs的新原点");
            PromptPointResult ppr = ed.GetPoint(opt);
            if(ppr.Status!=PromptStatus.OK) return;//如果用户未输入有效点，则返回
            Point3d pt = ppr.Value;
            using (var tr=db.TransactionManager.StartTransaction())
            {
                var ucsId = db.GetCurrentUCS();//获取当前ucs
                ucsId.SetUCSOrigin(pt);
                ucsId.RotateUCS(60,Vector3d.ZAxis);//将ucs绕Z轴旋转60度
                tr.Commit();
            }
        }

        [CommandMethod("UCSCir")]
        public void UCSCir()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = db.GetEditor();
            Circle c1=new Circle(Point3d.Origin, Vector3d.ZAxis, 50);
            Circle c2=new Circle(Point3d.Origin, Vector3d.ZAxis, 80);
            var mt = ed.CurrentUserCoordinateSystem;//得到当前ucs矩阵
            c2.TransformBy(mt);
            using (var tr=db.TransactionManager.StartTransaction())
            {
                db.AddToModelSpace(c1, c2);
                tr.Commit();
            }
        }
    }
}
